package com.bilibili.service.impl;

import com.alibaba.fastjson.JSON;
import com.bilibili.dto.VideoToJedis;
import com.bilibili.entity.BulletChat;
import com.bilibili.entity.Video;
import com.bilibili.exception.BusinessException;
import com.bilibili.mapper.VideoMapper;
import com.bilibili.service.VideoService;
import com.bilibili.utils.RedisOperations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static com.bilibili.constant.BusinessConstant.*;

@Service
public class VideoServiceImpl implements VideoService {
    @Autowired
    private VideoMapper videoMapper;
    @Autowired
    private RedisOperations redisOperations;


    @Override
    public List<Video> getRandomVideo() {
        List<Integer> ids=new ArrayList<>();
     //TODO 获取随机videoid
        ids.add(1);
        List<Video> randomVideo = videoMapper.getRandomVideo(ids);
        return randomVideo;
    }

    @Override
    public Video getVideoById(Integer id) {
        Video videoById = null;
        if(!Objects.isNull(videoMapper.getVideoById(id)))
        {
             videoById = videoMapper.getVideoById(id);
        }
        return videoById;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
    public void syncRedisCompletequestionToMySql() {
        String redisToMysql = redisOperations.get(REDIS_COMPLETE_VIDEO_KEY);
        List<VideoToJedis> videoToJedis = JSON.parseArray(redisToMysql, VideoToJedis.class);
        System.out.println(videoToJedis);
        /**
         * 拿到redis缓存里的数量
         */
        List<Integer> ids = new ArrayList<>();
        for (int i = 0; i < videoToJedis.size(); i++) {
            ids.add(videoToJedis.get(i).getId());
           Integer number=videoMapper.updateVideoToRedisByid(videoToJedis.get(i));
            System.out.println(number);
            /**
             * 更新mysql对应id的数据
             */
        }
    }
    @Override
    public VideoToJedis getVideoToJedisByid(Integer videoid) {
        VideoToJedis videobyJedis = new VideoToJedis();
        if(!Objects.isNull(videoid)) {
            String redisToMysql = redisOperations.get(REDIS_COMPLETE_VIDEO_KEY);

            List<VideoToJedis> videoToJedis = JSON.parseArray(redisToMysql, VideoToJedis.class);
            System.out.println(videoToJedis);
            for (int i = 0; i < videoToJedis.size(); i++) {
                if (videoid.equals(videoToJedis.get(i).getVideoid())) {
                    videobyJedis = videoToJedis.get(i);
                }
            }
        }else{
            throw new BusinessException(HttpStatus.BAD_REQUEST, "Redis查询失败");
        }
        System.out.println(videobyJedis);
        return videobyJedis;
    }

    @Override
    public List<BulletChat> getBulletChatByid(Integer videoid) {
        return videoMapper.getBulletChatByid(videoid);
    }

    @Override
    public Boolean addBulletChat(BulletChat bulletChat) {
        return videoMapper.addBulletChat(bulletChat)>0;
    }
}
